<!DOCTYPE html>
<html>
<head hexo-theme='https://volantis.js.org/#2.6.6'>
  <meta charset="utf-8">
  <!-- SEO相关 -->
  
    
  
  <!-- 渲染优化 -->
  <meta name="renderer" content="webkit">
  <meta name="force-rendering" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="HandheldFriendly" content="True" >
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  <!-- 页面元数据 -->
  
    <title>C++程序设计基础(5) - 复合类型数据：指针与引用初步 - Jun&#39;s Blog</title>
  
    <meta name="keywords" content="C++">
  
  
    <meta name="description" content="指针和引用类型的初步介绍, 更深入的内容留到后面再讲.">
  

  <!-- feed -->
  
    <link rel="alternate" href="/junblog/atom.xml" title="Jun's Blog">
  

  <!-- import meta -->
  

  <!-- link -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13/css/all.min.css">
  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">

  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.css">

  

  
  <link rel="shortcut icon" type='image/x-icon' href="https://cdn.jsdelivr.net/gh/xaoxuu/assets@master/favicon/favicon.ico">
  

  

  

  <!-- import link -->
  

  
  
    
<link rel="stylesheet" href="/junblog/css/style.css">

  

  <script>
    function setLoadingBarProgress(num) {
      document.getElementById('loading-bar').style.width=num+"%";
    }
  </script>

  
  
</head>

<body>
  
  <div id="loading-bar-wrapper">
  <div id="loading-bar"></div>
</div>
<header class="l_header shadow blur">
  <div class='container'>
  <div class='wrapper'>
    <div class='nav-sub'>
      <p class="title"></p>
      <ul class='switcher nav-list-h'>
        <li><a class="s-comment fas fa-comments fa-fw" target="_self" href='javascript:void(0)'></a></li>
        
          <li><a class="s-toc fas fa-list fa-fw" target="_self" href='javascript:void(0)'></a></li>
        
      </ul>
    </div>
		<div class="nav-main">
      
        
        <a class="title flat-box" target="_self" href='/junblog/'>
          
          
          
            Jun's Blog
          
          
        </a>
      

			<div class='menu navigation'>
				<ul class='nav-list-h'>
          
          
          
            
            
              <li>
                <a class="flat-box" href=/junblog/
                  
                  
                  
                    id="junblog"
                  >
                  <i class='fas fa-rss fa-fw'></i>首页
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/junblog/categories/
                  
                  
                  
                    id="junblogcategories"
                  >
                  <i class='fas fa-folder-open fa-fw'></i>分类
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/junblog/tags/
                  
                  
                  
                    id="junblogtags"
                  >
                  <i class='fas fa-tags fa-fw'></i>标签
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/junblog/archives/
                  
                  
                  
                    id="junblogarchives"
                  >
                  <i class='fas fa-archive fa-fw'></i>归档
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/junblog/friends/
                  
                  
                  
                    id="junblogfriends"
                  >
                  <i class='fas fa-link fa-fw'></i>友链
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/junblog/aboutme/
                  
                  
                  
                    id="junblogaboutme"
                  >
                  <i class='fas fa-info-circle fa-fw'></i>关于
                </a>
                
              </li>
            
          
          
				</ul>
			</div>

      <div class="m_search">
        <form name="searchform" class="form u-search-form">
          <i class="icon fas fa-search fa-fw"></i>
          <input type="text" class="input u-search-input" placeholder="Search..." />
        </form>
      </div>

			<ul class='switcher nav-list-h'>
				
					<li><a class="s-search fas fa-search fa-fw" target="_self" href='javascript:void(0)'></a></li>
				
				<li>
          <a class="s-menu fas fa-bars fa-fw" target="_self" href='javascript:void(0)'></a>
          <ul class="menu-phone list-v navigation white-box">
            
              
            
              <li>
                <a class="flat-box" href=/junblog/
                  
                  
                  
                    id="junblog"
                  >
                  <i class='fas fa-rss fa-fw'></i>首页
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/junblog/categories/
                  
                  
                  
                    id="junblogcategories"
                  >
                  <i class='fas fa-folder-open fa-fw'></i>分类
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/junblog/tags/
                  
                  
                  
                    id="junblogtags"
                  >
                  <i class='fas fa-tags fa-fw'></i>标签
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/junblog/archives/
                  
                  
                  
                    id="junblogarchives"
                  >
                  <i class='fas fa-archive fa-fw'></i>归档
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/junblog/friends/
                  
                  
                  
                    id="junblogfriends"
                  >
                  <i class='fas fa-link fa-fw'></i>友链
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/junblog/aboutme/
                  
                  
                  
                    id="junblogaboutme"
                  >
                  <i class='fas fa-info-circle fa-fw'></i>关于
                </a>
                
              </li>
            
          
            
          </ul>
        </li>
			</ul>
		</div>
	</div>
  </div>
</header>

<script>setLoadingBarProgress(40);</script>



  <div class="l_body nocover">
    <div class='body-wrapper'>
      

<div class='l_main'>
  

  
    <article id="post" class="post white-box reveal blur article-type-post" itemscope itemprop="blogPost">
      


  <section class='meta'>
    
      
      
      <div class="meta" id="header-meta">
        
          
  <h1 class="title">
    <a href="/junblog/2020/08/20/C++%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80(5)%20-%20%E5%A4%8D%E5%90%88%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE_%E6%8C%87%E9%92%88%E4%B8%8E%E5%BC%95%E7%94%A8%E5%88%9D%E6%AD%A5/">
      C++程序设计基础(5) - 复合类型数据：指针与引用初步
    </a>
  </h1>


        
        <div class='new-meta-box'>
          
            
          
            
              
<div class='new-meta-item author'>
  <a href="" rel="nofollow">
    <img src="https://cdn.jsdelivr.net/gh/JunyuanHe/cdn-assets@master/img/GeographicObjects2.jpg">
    <p>Jun</p>
  </a>
</div>

            
          
            
              
  
  <div class='new-meta-item category'>
    <a href='/junblog/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA/C/' rel="nofollow">
      <i class="fas fa-folder-open fa-fw" aria-hidden="true"></i>
      <p>计算机/C++</p>
    </a>
  </div>


            
          
            
              <div class="new-meta-item date">
  <a class='notlink'>
    <i class="fas fa-calendar-alt fa-fw" aria-hidden="true"></i>
    <p>发布于：2020年8月20日 20:01</p>
  </a>
</div>

            
          
            
              

            
          
        </div>
        
          <hr>
        
      </div>
    
  </section>


      <section class="article typo">
        <div class="article-entry" itemprop="articleBody">
          
          
          <p>指针和引用类型的初步介绍, 更深入的内容留到后面再讲.</p>
<a id="more"></a>
<h2 id="指针">指针</h2>
<p><strong>指针就是地址</strong>. 所有的变量都在内存中有一个地址, 通过地址可以使用变量, 我们就把这些地址形象地称为指向变量的指针.</p>
<h3 id="指针的最基本用法">指针的最基本用法:</h3>
<p><strong>(1) 指针变量的定义</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a;</span><br><span class="line"><span class="keyword">int</span> *p;  <span class="comment">//定义指针变量p, 指向int型变量. 称p为int*类型</span></span><br><span class="line">p = &amp;a;  <span class="comment">//&amp;表示取地址. 此句表明将a的地址赋给p.</span></span><br><span class="line">*p = <span class="number">3</span>;  <span class="comment">//使用p指向的变量</span></span><br></pre></td></tr></table></figure>
<p>注: 行2中和行4中星号的含义不同. 行2中是指针变量的定义, 星号表示该变量是一个指针变量, 即<code>int*</code>类型, 等价的写法是<code>int* p</code>(空格位置不影响), 但是我们提倡将<code>*</code>跟随变量名而不是类型名. 行4中星号表示取指针指向的变量.</p>
<p><strong>(2) 一次定义多个指针变量, 同时定义变量和指针</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a, *p = &amp;a, *q, b, *r = <span class="number">0</span>;</span><br></pre></td></tr></table></figure>
<p>在这里, <code>a</code>, <code>b</code>为int型变量. <code>p</code>为int型指针, 指向<code>a</code>, 注意这里初值<code>&amp;a</code>赋给<code>p</code>, 而不是赋给<code>*p</code>, 等价于<code>int *p; p = &amp;a;</code>. <code>q</code>为int型指针, 没有指向任何变量, 称为野指针, 不可使用<code>*p</code>. <code>r</code>是0指针，不指向任何变量。使用0指针指向的变量是非法的。</p>
<p><strong>(3) 指针变量的指向类型不同, 使用结果也不同</strong></p>
<p>我们来看下面例子:</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">float</span> f = <span class="number">5.0f</span>, *pf = &amp;f;</span><br><span class="line"><span class="keyword">int</span> *pi = (<span class="keyword">int</span> *) &amp;f;</span><br><span class="line"><span class="built_in">cout</span> &lt;&lt; *pf &lt;&lt; <span class="string">"\t"</span> &lt;&lt; *pi &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"><span class="comment">//结果: 5	1084227584</span></span><br></pre></td></tr></table></figure>
<p>这里, <code>pf</code>和<code>pi</code>保存的地址是相同的, 指向同一块二进制数据, 但是它们将这部分二进制数分别按int型解释和float型解释, 所以得到的结果不一样.</p>
<h3 id="指针的算术运算">指针的算术运算</h3>
<p>C++的指针可以进行算术运算, 但是指针的算术运算仅限于</p>
<p>①指针加/减一个整数, 表示以指向类型的存储单元为单位, 向前或向后移动若干个元素的位置. 如double型8个字节为一个单元, 若有定义<code>double a, *p = &amp;a;</code>则<code>p+1</code>表示的是<code>p</code>后面8个字节的位置.</p>
<p>②两个同类型的指针相减, 表示以指向类型的存储单元为单位, 两个地址之间相差的元素个数</p>
<h3 id="用指针处理数组">用指针处理数组</h3>
<p>指针的算术运算常常用来处理数组</p>
<p>在<strong>绝大多数情况下，数组名表示数组的首地址</strong>(数组第一个元素的地址)。即若定义<code>int a[]</code>则<code>a</code>等价于<code>&amp;a[0]</code>。</p>
<p>但是，需要注意的是，这里有<strong>两个例外: 第一个是<code>sizeof()</code>，第二个是取地址<code>&amp;</code></strong>。在这两种情况之下，我们不能把数组名等同于数组的首地址。</p>
<p>假设我们已经定义了<code>int a[10]</code>，那么</p>
<p>• <code>sizeof(a)</code>返回的是<strong>整个数组的长度</strong>40，而不是指向数组的指针的长度4。</p>
<p>• <code>&amp;a</code>得到的是一个<strong>指向数组的指针</strong>，而不是一个指向指针常量的指针。这里再要特别注意，是指向数组的指针，而不是指向数组第一个元素的指针。即<code>&amp;a</code>和<code>&amp;a+1</code>相差的是整个数组的长度，而<code>a</code>和<code>a+1</code>相差的只是一个数组元素的长度。</p>
<p>扫描数组的n个元素并进行处理有一下几种方式:</p>
<p>• 下标方式: <figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">	<span class="comment">//处理a[i]，或者处理*(a+i)</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>• 指针方式: <figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (p = a, <span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">	<span class="comment">//处理*p</span></span><br><span class="line">	<span class="comment">//此方法用下标控制循环，用指针处理数据</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>• 纯指针方式: <figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (p = a; p &lt; a + n; p++) &#123;</span><br><span class="line">	<span class="comment">//处理*p</span></span><br><span class="line">	<span class="comment">//此方法循环控制和数据处理均使用指针</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>利用好数组元素首地址这个指针常量，有时可以使得代码更加简洁。</p>
<h3 id="动态变量和动态数组">动态变量和动态数组</h3>
<p>用<code>new</code>运算符可以开辟一些临时变量和临时数组，不再使用时可以通过<code>delete</code>来释放空间。这样开辟的变量称为动态变量，开辟的数组称为动态数组它们没有名字，因此只能通过指针来使用它们。</p>
<p>动态数组的一个优势是它的长度可以使用变量给出(如<code>int *p, n=10; p = new int[n];</code>，而普通数组只可以使用常量<code>int a[10];</code>。这就为很多情况下程序的设计提供了方便。</p>
<p>下面是动态数组使用的一个例子 <figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> n = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">double</span> *p = <span class="keyword">new</span> <span class="keyword">double</span>[n];  <span class="comment">// 动态数组的定义</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) p[i] = i;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) <span class="built_in">cout</span> &lt;&lt; p[i] &lt;&lt; <span class="string">"\t"</span>;</span><br><span class="line"><span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"><span class="keyword">delete</span>[] p;  <span class="comment">//释放动态数组p</span></span><br></pre></td></tr></table></figure></p>
<h2 id="引用">引用</h2>
<p>引用就是取别名.</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a; <span class="keyword">int</span> &amp;b = a; <span class="keyword">int</span> &amp;c = b;</span><br><span class="line"><span class="keyword">int</span> b = <span class="number">5</span>;  <span class="comment">//等效于int a = 5;也等效于int c = 5;</span></span><br></pre></td></tr></table></figure>
<p>如上例, 我们先对<code>a</code>取了一个别名<code>b</code>, 于是<code>a</code>和<code>b</code>表示的是同一个变量. 我们接着对<code>b</code>取了一个别名叫<code>c</code>, 则<code>a</code>, <code>b</code>, <code>c</code>都表示同一个变量了. 我们称<code>b</code>, <code>c</code>都是<code>a</code>的引用.</p>
<p>取了别名后, 别名和原来的名字没有区别.</p>

          
            <div class='article_footer'>
              
                
  
    
    


  

<section class="widget list references  desktop mobile">
  
  <header>
    
      <i class="fas fa-quote-left fa-fw" aria-hidden="true"></i><span class='name'>参考资料</span>
    
  </header>


  <div class='content'>
    <ul class="list entry navigation">
      
        <li><a class="flat-box" title="http://product.dangdang.com/25239691.html" href="http://product.dangdang.com/25239691.html"
          
          
          id="http:productdangdangcom25239691html"
          >
          <div class='name'>
            
            邵荣. C++程序设计(第二版)
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="https://book.douban.com/subject/3012360/" href="https://book.douban.com/subject/3012360/"
          
          
          id="https:bookdoubancomsubject3012360"
          >
          <div class='name'>
            
            C和指针
          </div>
          
        </a></li>
      
    </ul>
  </div>
</section>



  

  
    
    



  

  
    
    

    <section class="widget copyright  desktop mobile">
        <div class='content'>
            
                <blockquote>
                    
                        
                                    <p>博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议。转载请保留原文链接和作者。</p>

                                        
                                            
                </blockquote>
                
        </div>
    </section>
  

  


              
            </div>
          
        </div>
        
          


  <section class='meta' id="footer-meta">
    <div class='new-meta-box'>
      
        
          <div class="new-meta-item date" itemprop="dateUpdated" datetime="2020-08-21T23:10:22+08:00">
  <a class='notlink'>
    <i class="fas fa-edit fa-fw" aria-hidden="true"></i>
    <p>更新于：2020年8月21日 23:10</p>
  </a>
</div>

        
      
        
          
  
  <div class="new-meta-item meta-tags"><a class="tag" href="/junblog/tags/C/" rel="nofollow"><i class="fas fa-hashtag fa-fw" aria-hidden="true"></i><p>C++</p></a></div>


        
      
        
          
  <div class="new-meta-item share -mob-share-list">
  <div class="-mob-share-list share-body">
    
      
        <a class="-mob-share-qq" title="" rel="external nofollow noopener noreferrer"
          
          href="http://connect.qq.com/widget/shareqq/index.html?url=https://heliumhe.gitee.io/2020/08/20/C++%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80(5)%20-%20%E5%A4%8D%E5%90%88%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE_%E6%8C%87%E9%92%88%E4%B8%8E%E5%BC%95%E7%94%A8%E5%88%9D%E6%AD%A5/&title=C++程序设计基础(5) - 复合类型数据：指针与引用初步 - Jun's Blog&summary=指针和引用类型的初步介绍, 更深入的内容留到后面再讲."
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/qq.png">
          
        </a>
      
    
      
        <a class="-mob-share-qzone" title="" rel="external nofollow noopener noreferrer"
          
          href="https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://heliumhe.gitee.io/2020/08/20/C++%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80(5)%20-%20%E5%A4%8D%E5%90%88%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE_%E6%8C%87%E9%92%88%E4%B8%8E%E5%BC%95%E7%94%A8%E5%88%9D%E6%AD%A5/&title=C++程序设计基础(5) - 复合类型数据：指针与引用初步 - Jun's Blog&summary=指针和引用类型的初步介绍, 更深入的内容留到后面再讲."
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/qzone.png">
          
        </a>
      
    
      
        <a class="-mob-share-weibo" title="" rel="external nofollow noopener noreferrer"
          
          href="http://service.weibo.com/share/share.php?url=https://heliumhe.gitee.io/2020/08/20/C++%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80(5)%20-%20%E5%A4%8D%E5%90%88%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE_%E6%8C%87%E9%92%88%E4%B8%8E%E5%BC%95%E7%94%A8%E5%88%9D%E6%AD%A5/&title=C++程序设计基础(5) - 复合类型数据：指针与引用初步 - Jun's Blog&summary=指针和引用类型的初步介绍, 更深入的内容留到后面再讲."
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/weibo.png">
          
        </a>
      
    
      
        
        <div class='hoverbox'>
          <a><img src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/wechat.png"></a>
          <div class='target'>
            <img src="">
          </div>
        </div>
      
    
  </div>
</div>



        
      
    </div>
  </section>


        
        
          <div class="prev-next">
            
              <a class='prev' href='/junblog/2020/08/22/C++%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80(6)%20-%20%E5%87%BD%E6%95%B0/'>
                <p class='title'><i class="fas fa-chevron-left" aria-hidden="true"></i>C++程序设计基础(6) - 函数</p>
                <p class='content'>C++函数的核心知识

核心知识(自测)
函数的定义

函数定义、函数调用、函数说明的形式
函数定义的位置（可不可以放在另一个函数的函数体内？不可以）。函数说明什么时候可以省略？（先...</p>
              </a>
            
            
              <a class='next' href='/junblog/2020/08/16/C++%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E5%9F%BA%E7%A1%80(4)%20-%20%E5%A4%8D%E5%90%88%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE_%E6%95%B0%E7%BB%84/'>
                <p class='title'>C++程序设计基础(4) - 复合类型数据：数组<i class="fas fa-chevron-right" aria-hidden="true"></i></p>
                <p class='content'>介绍数组和C风格字符串的处理, 以及一些十分常用的数组算法.

一维数组
• 数组的定义:
1int a[10]; double b[20]; char c[80];
数组的大小必须是...</p>
              </a>
            
          </div>
        
      </section>
    </article>
  

  
    <!-- 显示推荐文章和评论 -->



  <article class="post white-box reveal comments blur">
    <section class="article typo">
      <p ct><i class='fas fa-comments'></i> 评论</p>
      
        <p cst>读者们可以在此处进行交流</p>
      
      
      
      
      
      
        <section id="comments">
          <div id="valine_container" class="valine_thread">
            <i class="fas fa-cog fa-spin fa-fw fa-2x"></i>
          </div>
        </section>
      
      
    </section>
  </article>


  




<!-- 根据页面mathjax变量决定是否加载MathJax数学公式js -->



  <script>
    window.subData = {
      title: 'C++程序设计基础(5) - 复合类型数据：指针与引用初步',
      tools: true
    }
  </script>


</div>
<aside class='l_side'>
  
  
    
    



  <section class="widget toc-wrapper blur desktop mobile" id="toc-div" >
    
  <header>
    
      <i class="fas fa-list fa-fw" aria-hidden="true"></i><span class='name'>本文目录</span>
    
  </header>


    <div class='content'>
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#指针"><span class="toc-number">1.</span> <span class="toc-text">指针</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#指针的最基本用法"><span class="toc-number">1.1.</span> <span class="toc-text">指针的最基本用法:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#指针的算术运算"><span class="toc-number">1.2.</span> <span class="toc-text">指针的算术运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#用指针处理数组"><span class="toc-number">1.3.</span> <span class="toc-text">用指针处理数组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#动态变量和动态数组"><span class="toc-number">1.4.</span> <span class="toc-text">动态变量和动态数组</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#引用"><span class="toc-number">2.</span> <span class="toc-text">引用</span></a></li></ol>
    </div>
  </section>


  


</aside>


  
  <footer class="clearfix">
    <br><br>
    
      
        <div class="aplayer-container">
          


        </div>
      
    
      
        <br>
        <div class="social-wrapper">
          
            
              <a href="/junblog/atom.xml"
                class="social fas fa-rss flat-btn"
                target="_blank"
                rel="external nofollow noopener noreferrer">
              </a>
            
          
            
              <a href="mailto:hejunyuan02@126.com"
                class="social fas fa-envelope flat-btn"
                target="_blank"
                rel="external nofollow noopener noreferrer">
              </a>
            
          
            
              <a href="https://music.163.com/#/user/home?id=63035382"
                class="social fas fa-headphones-alt flat-btn"
                target="_blank"
                rel="external nofollow noopener noreferrer">
              </a>
            
          
        </div>
      
    
      
        本站使用
        <a href="https://volantis.js.org/" target="_blank" class="codename">Volantis</a>
        作为主题，总访问量为
          <span id="busuanzi_value_site_pv"><i class="fas fa-circle-notch fa-spin fa-fw" aria-hidden="true"></i></span>
          次
        
      
    
      
        <div class='copyright'>
        <p><a href="https://junyuanhe.github.io/" target="_blank" rel="noopener">Copyright © 2019-2020 Jun</a></p>

        </div>
      
    
  </footer>

<script>setLoadingBarProgress(80);</script>


      <script>setLoadingBarProgress(60);</script>
    </div>
    <a class="s-top fas fa-arrow-up fa-fw" href='javascript:void(0)'></a>
  </div>
  
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4/dist/jquery.min.js"></script>


  <script>
    
    var SEARCH_SERVICE = "hexo" || "hexo";
    var ROOT = "/junblog/" || "/";
    if (!ROOT.endsWith('/')) ROOT += '/';
  </script>





  <script async src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-volantis@2/js/instant_page.js" type="module" defer integrity="sha384-OeDn4XE77tdHo8pGtE1apMPmAipjoxUQ++eeJa6EtJCfHlvijigWiJpD7VDPWXV1"></script>


  <script src="https://cdn.jsdelivr.net/npm/scrollreveal@4.0.6/dist/scrollreveal.min.js"></script>
  <script type="text/javascript">
    $(function() {
      ScrollReveal().reveal('.l_main .reveal', {
        distance: '8px',
        duration: '800',
        interval: '100',
        scale: '1'
      });
    });
  </script>


  
<script src="https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.js"></script>

  <script type="text/javascript">
    $(function() {
      Waves.attach('.flat-btn', ['waves-button']);
      Waves.attach('.float-btn', ['waves-button', 'waves-float']);
      Waves.attach('.float-btn-light', ['waves-button', 'waves-float', 'waves-light']);
      Waves.attach('.flat-box', ['waves-block']);
      Waves.attach('.float-box', ['waves-block', 'waves-float']);
      Waves.attach('.waves-image');
      Waves.init();
    });
  </script>


  <script defer src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-busuanzi@2.3/js/busuanzi.pure.mini.js"></script>



  
  
  
    
<script src="https://cdn.jsdelivr.net/npm/jquery-backstretch@2.1.18/jquery.backstretch.min.js"></script>

    <script type="text/javascript">
      $(function(){
        var imgs=["https://cdn.jsdelivr.net/gh/xaoxuu/cdn-wallpaper/abstract/41F215B9-261F-48B4-80B5-4E86E165259E.jpeg", "https://cdn.jsdelivr.net/gh/xaoxuu/cdn-wallpaper/abstract/BBC19066-E176-47C2-9D22-48C81EE5DF6B.jpeg", "https://cdn.jsdelivr.net/gh/xaoxuu/cdn-wallpaper/abstract/B18FCBB3-67FD-48CC-B4F3-457BA145F17A.jpeg", "https://cdn.jsdelivr.net/gh/xaoxuu/cdn-wallpaper/abstract/67239FBB-E15D-4F4F-8EE8-0F1C9F3C4E7C.jpeg"];
        if ('true' == 'true') {
          function shuffle(arr){
            /*From countercurrent-time*/
            var n = arr.length;
            while(n--) {
              var index = Math.floor(Math.random() * n);
              var temp = arr[index];
              arr[index] = arr[n];
              arr[n] = temp;
            }
          }
          shuffle(imgs);
        }
        if ('.cover') {
          $('.cover').backstretch(
            imgs,
          {
            duration: "20000",
            fade: "1500"
          });
        } else {
          $.backstretch(
            imgs,
          {
            duration: "20000",
            fade: "1500"
          });
        }
      });
    </script>
  











  
    
<script src="/junblog/js/valine.js"></script>

  
  <script>
  var GUEST_INFO = ['nick','mail','link'];
  var meta = 'nick,mail'.split(',').filter(function(item){
    return GUEST_INFO.indexOf(item) > -1
  });
  var REQUIRED_FIELDS = ['nick','mail','link'];
  var requiredFields = 'nick,mail'.split(',').filter(function(item){
    return REQUIRED_FIELDS.indexOf(item) > -1
  });
  var valine = new Valine();
  function emoji(path, idx, ext) {
      return path + "/" + path + "-" + idx + "." + ext;
  }
  var emojiMaps = {};
  for (var i = 1; i <= 54; i++) {
    emojiMaps['tieba-' + i] = emoji('tieba', i, 'png');
  }
  for (var i = 1; i <= 101; i++) {
    emojiMaps['qq-' + i] = emoji('qq', i, 'gif');
  }
  for (var i = 1; i <= 116; i++) {
    emojiMaps['aru-' + i] = emoji('aru', i, 'gif');
  }
  for (var i = 1; i <= 125; i++) {
    emojiMaps['twemoji-' + i] = emoji('twemoji', i, 'png');
  }
  for (var i = 1; i <= 4; i++) {
    emojiMaps['weibo-' + i] = emoji('weibo', i, 'png');
  }
  valine.init({
    el: '#valine_container',
    meta: meta,
    
    appId: "GbPm6UIzHrGduLaANgBIdhkf-gzGzoHsz",
    appKey: "SFuacIL4IJSnGk9TDX46eb0Y",
    placeholder: "快来评论吧~",
    pageSize:'10',
    avatar:'robohash',
    lang:'zh-cn',
    visitor: 'true',
    highlight: 'true',
    mathJax: 'true',
    enableQQ: 'true',
    requiredFields: requiredFields,
    emojiCDN: 'https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/emoji/valine/',
    emojiMaps: emojiMaps
  })
  </script>





  
<script src="/junblog/js/app.js"></script>



  
<script src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-volantis@2.6.5/js/search.js"></script>







<!-- 复制 -->

  <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="fas fa-copy"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('fa-copy');
        $icon.addClass('fa-check-circle');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('fa-check-circle');
          $icon.addClass('fa-copy');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('fa-copy');
        $icon.addClass('fa-times-circle');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('fa-times-circle');
          $icon.addClass('fa-copy');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>




<!-- fancybox -->
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
<script>
  function pjax_fancybox() {
    $(".article-entry").find("img").not('.inline').not('a img').each(function () { //渲染 fancybox
      var element = document.createElement("a"); // a 标签
      $(element).attr("pjax-fancybox", "");  // 过滤 pjax
      $(element).attr("href", $(this).attr("src"));
      if ($(this).attr("data-original")) {
        $(element).attr("href", $(this).attr("data-original"));
      }
      $(element).attr("data-fancybox", "images");
      var caption = "";   // 描述信息
      if ($(this).attr('alt')) {  // 标准 markdown 描述信息
        $(element).attr('data-caption', $(this).attr('alt'));
        caption = $(this).attr('alt');
      }
      var div = document.createElement("div");
      $(div).addClass("fancybox");
      $(this).wrap(div); // 最外层套 div ，其实主要作用还是 class 样式
      var span = document.createElement("span");
      $(span).addClass("image-caption");
      $(span).text(caption); // 加描述
      $(this).after(span);  // 再套一层描述
      $(this).wrap(element);  // 最后套 a 标签
    })
    $(".article-entry").find("img").fancybox({
      selector: '[data-fancybox="images"]',
      hash: false,
      loop: false,
      closeClick: true,
      helpers: {
        overlay: {closeClick: true}
      },
      buttons: [
        "zoom",
        "close"
      ]
    });
  };
  $(function () {
    pjax_fancybox();
  });
</script>





  <script>setLoadingBarProgress(100);</script>
</body>
</html>
